package com.retoc.workflowable.controller;

import com.retoc.workflowable.pojo.MeetingReq;
import org.flowable.engine.*;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/meeting")
public class MeetingController {

    @Autowired
    ProcessEngine processEngine;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    HistoryService historyService;

    @PostMapping("/meetingRequest")
    public HashMap<String, Object> meetingCreate(@Valid @RequestBody MeetingReq req){
        Map<String,Object> map=new HashMap<>();
        map.put("url", req.getUrl());
        map.put("createrId", req.getCreaterId());
        map.put("managerId", req.getManagerId());
        map.put("gmId", req.getGmId());
        map.put("needGm", req.getNeedGm());
        map.put("date", req.getDates());
//        map.put("hours", req.getHours());
        map.put("types", req.getTypes());
        map.put("uuid", req.getUuid());

        ProcessInstance instance = runtimeService.startProcessInstanceByKey("meetingReq", map);
//        leaveReq.getId()
        Task task = taskService.createTaskQuery()
                .processInstanceId(instance.getId())
                .taskAssignee(req.getCreaterId()+"")
                .singleResult();

        taskService.complete(task.getId());
        HashMap<String,Object> result=new HashMap<>();
        result.put("instanceId",instance.getId());
        result.put("types",req.getTypes());
        result.put("uuid",req.getUuid());
        return result;
    }

//    //删除请假申请
//    @DeleteMapping("/DeleteProcessById")
//    public R DeleteProcessById(@Valid @RequestBody DeleteReq req){
//        Task task = taskService.createTaskQuery().processInstanceId(req.getInstanceId()).singleResult();
//        if (task==null){
//            return R.error("实例ID错误！");
//        }
//        runtimeService.deleteProcessInstance(req.getInstanceId(),req.getReason());
////        System.out.println(req.getInstanceId()+"删除成功，原因为："+req.getReason());
//        return R.ok();
//    }

}
